package xyz.bali16.module.netdisk.controller.utils;

/**
 * @author bali2 on 7/11/2021.
 * @version 1.0
 */

import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import io.swagger.annotations.ApiOperation;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.core.utils.AuthUtils;

import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.netdisk.entity.FileUser;
import xyz.bali16.module.netdisk.service.FileFolderService;
import xyz.bali16.module.netdisk.service.FileStorageService;
import xyz.bali16.module.netdisk.service.FileUserService;
import xyz.bali16.module.netdisk.service.NetdiskService;
import xyz.bali16.module.netdisk.utils.PageUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;


/**
 * 用来装载调用网盘者的相关信息
 */

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Component
@Getter
@Setter
public class NetdiskArticleCallObj {
    @Autowired
    private NetdiskService netdiskService;

    @Autowired
    @AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    private UserService userService;
    @Autowired
    private FileUserService fileUserService;
    @Autowired
    private FileFolderService fileFolderService;
    @Autowired
    private FileStorageService fileStorageService;
    private String fileUserId = "80";
    private String fileStorageId = "80";
    private String fileFolderId = "80";


    @ApiOperation("上传到个人空间")
    public Result<String> myUpload(MultipartFile file, HttpServletRequest request) {
        String username = AuthUtils.getCurrentUsername();
        User user = userService.getByUsername(username);
        FileUser fileUser = fileUserService.getFileUserBySysUserId(user.getUserId());
         // 需要做是否存在fileUser检验
        System.out.println("netdiskService:");
        System.out.println(netdiskService);
        return netdiskService.uploadFile(file, fileUser.getId(), fileStorageId, fileFolderId, request);
    }

    public Result<String> upload(MultipartFile file, HttpServletRequest request) {
        System.out.println("netdiskService:");
        System.out.println(netdiskService);
        return netdiskService.uploadFile(file, fileUserId, fileStorageId, fileFolderId, request);
    }

    public void download(String fileId, HttpServletRequest request, HttpServletResponse response) {
        netdiskService.downloadFile(fileUserId, fileStorageId, fileId, request, response);
    }

    public PageUtils list(Integer curPage, Integer pageSize) {
        return netdiskService.list(fileStorageId, fileFolderId, curPage, pageSize);
    }

    public boolean delete(String fileId) {
        return netdiskService.deleteFile(fileId);
    }

}
